﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Classes.*;
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import __AS3__.vec.*;
    import pixelShader.*;
    import Engine.*;
    import com.adobe.utils.*;

    public class ItemImage extends EventDispatcher {

        private static var s_cached:uint = 0;
        private static var s_total:uint = 0;
        private static var m_directionCodes:Array = ["SW", "SE", "NE", "NW", "N", "E", "S", "W", "SWW", "NWW", "NNW", "NNE", "NEE", "SEE", "SSE", "SSW"];
        private static var countAll:int = 0;

        public var name:String;
        public var type:String = "";
        public var lowResImage:Object = null;
        public var is_root:Boolean = false;
        public var is_road:Boolean = false;
        public var forcedWidth:Number = 0;
        public var forcedHeight:Number = 0;
        public var offsetX:Number = 0;
        public var offsetY:Number = 0;
        public var direction:int = 0;
        public var allDirections:Boolean = false;
        public var phase:String;
        public var allPhases:Boolean = false;
        private var m_hotspotMap:Dictionary;
        private var m_hotspots:Vector.<Vector3>;
        private var m_xml:XML;
        private var m_loaded:Boolean;
        private var m_loadStarted:Boolean;
        private var m_assets:Array;
        private var m_images:Array;
        private var m_unloadedAssets:Array;
        private var m_className:String;
        private var counter:int = 0;

        public function ItemImage(_arg1:XML, _arg2:Boolean=false):void{
            this.m_xml = _arg1;
            if (_arg2){
                this.lazy_init();
            };
            this.m_className = String(_arg1.@className);
            this.forcedWidth = int(_arg1.@width);
            this.forcedHeight = int(_arg1.@height);
            this.offsetX = int(_arg1.@offsetX);
            this.offsetY = int(_arg1.@offsetY);
            this.name = String(_arg1.@name);
            this.type = String(_arg1.@type);
            var _local3:String = String(_arg1.@direction);
            var _local4:int;
            while (_local4 < Item.DIRECTION_16_MAX) {
                if (m_directionCodes[_local4] == _local3){
                    this.direction = _local4;
                    break;
                };
                _local4++;
            };
            if (_local3 == "ALL"){
                this.allDirections = true;
            };
            var _local5:String = String(_arg1.@phase);
            if (((!((_local5 == null))) && (!((_local5 == ""))))){
                this.phase = _local5;
            } else {
                this.allPhases = true;
            };
        }
        public static function getDirectionCode(_arg1:int):String{
            var _local2:String;
            if (((m_directionCodes) && ((m_directionCodes.length > _arg1)))){
                _local2 = m_directionCodes[_arg1];
            };
            return (_local2);
        }

        public function getHotspotsByName(_arg1:String):Vector.<Vector3>{
            var _local2:XML;
            var _local3:XML;
            var _local4:int;
            var _local5:String;
            var _local6:Vector.<Vector3>;
            var _local7:int;
            var _local8:XML;
            if (this.m_hotspotMap == null){
                _local2 = this.m_xml;
                this.m_hotspotMap = new Dictionary();
                if (_local2.hotspots.length() > 0){
                    for each (_local3 in _local2.hotspots) {
                        _local4 = _local3.hotspot.length();
                        _local5 = _local3.@name.toString();
                        if (_local4 > 0){
                            _local6 = new Vector.<Vector3>(_local4);
                            _local7 = 0;
                            while (_local7 < _local4) {
                                _local8 = _local3.hotspot[_local7];
                                _local6[_local7] = new Vector3(Number(_local8.@x), Number(_local8.@y));
                                _local7++;
                            };
                            this.m_hotspotMap[_local5] = _local6;
                            if (_local5 == ""){
                                this.m_hotspots = _local6;
                            };
                        } else {
                            if (_local5 == ""){
                                this.m_hotspots = Vector.<Vector3>([new Vector3(Number(_local2.@hotX), Number(_local2.@hotY))]);
                                this.m_hotspotMap[""] = this.m_hotspots;
                            };
                        };
                    };
                };
                if (!this.m_hotspots){
                    this.m_hotspots = Vector.<Vector3>([new Vector3(Number(_local2.@hotX), Number(_local2.@hotY))]);
                    this.m_hotspotMap[""] = this.m_hotspots;
                };
            };
            if (((!((this.m_hotspotMap[_arg1] == null))) && ((this.m_hotspotMap[_arg1] is Vector.<Vector3>)))){
                return (this.m_hotspotMap[_arg1]);
            };
            return (this.m_hotspots);
        }
        protected function lazy_init():void{
            var _local1:XML;
            var _local2:String;
            var _local3:int;
            var _local4:int;
            var _local5:ItemImageAsset;
            var _local6:ItemImage;
            if (this.m_assets == null){
                this.m_assets = [];
                this.m_images = [];
                this.m_unloadedAssets = [];
                _local1 = this.m_xml;
                _local2 = _local1.@url;
                if (_local2.toString().length > 0){
                    this.m_assets.push(new ItemImageAsset(_local1));
                };
                _local3 = 0;
                while (_local3 < _local1.asset.length()) {
                    _local5 = new ItemImageAsset(_local1.asset[_local3]);
                    this.m_assets.push(_local5);
                    _local3++;
                };
                _local4 = 0;
                while (_local4 < _local1.image.length()) {
                    _local6 = new ItemImage(_local1.image[_local4], true);
                    this.m_images.push(_local6);
                    _local4++;
                };
            };
        }
        public function load():void{
            var _local1:int;
            var _local2:int;
            var _local3:IEventDispatcher;
            var _local4:Array;
            var _local5:*;
            if ((((this.m_loaded == false)) && ((this.m_loadStarted == false)))){
                this.lazy_init();
                this.m_loadStarted = true;
                _local1 = 0;
                while (_local1 < this.m_assets.length) {
                    this.m_unloadedAssets.push(this.m_assets[_local1]);
                    _local1++;
                };
                _local2 = 0;
                while (_local2 < this.m_images.length) {
                    this.m_unloadedAssets.push(this.m_images[_local2]);
                    _local2++;
                };
                for each (_local3 in this.m_unloadedAssets) {
                    _local3.addEventListener(LoaderEvent.LOADED, this.onAssetLoaded);
                };
                _local4 = ArrayUtil.copyArray(this.m_unloadedAssets);
                for each (_local5 in _local4) {
                    _local5.load();
                };
            };
        }
        protected function onAssetLoaded(_arg1:Event):void{
            var _local3:IEventDispatcher;
            _arg1.stopImmediatePropagation();
            var _local2:int;
            while (_local2 < this.m_unloadedAssets.length) {
                _local3 = (this.m_unloadedAssets[_local2] as IEventDispatcher);
                if (_local3 == _arg1.target){
                    _local3.removeEventListener(LoaderEvent.LOADED, this.onAssetLoaded);
                    this.m_unloadedAssets.splice(_local2, 1);
                    break;
                };
                _local2++;
            };
            if (this.m_unloadedAssets.length == 0){
                this.m_loaded = true;
                dispatchEvent(new LoaderEvent(LoaderEvent.LOADED));
            };
        }
        public function isLoaded():Boolean{
            return (this.m_loaded);
        }
        public function isLoading():Boolean{
            return (((this.m_loadStarted) && (!(this.m_loaded))));
        }
        public function isMatching(_arg1:String, _arg2:int, _arg3:String):Boolean{
            if (_arg1 != this.name){
                return (false);
            };
            if (((!(this.allDirections)) && (!((this.direction == _arg2))))){
                return (false);
            };
            if (((!(this.allPhases)) && (!((this.phase == _arg3))))){
                return (false);
            };
            return (true);
        }
        protected function getAssetByName(_arg1:String):ItemImageAsset{
            var _local2:ItemImageAsset;
            var _local4:ItemImageAsset;
            var _local3:int;
            while (_local3 < this.m_assets.length) {
                _local4 = (this.m_assets[_local3] as ItemImageAsset);
                if (_local4.name == _arg1){
                    _local2 = _local4;
                    break;
                };
                _local3++;
            };
            return (_local2);
        }
        public function getInstance(_arg1:MapResource=null):ItemImageInstance{
            var _local3:MovieClip;
            var _local2:ItemImageInstance = new ItemImageInstance();
            if (!this.isLoaded()){
                this.load();
            };
            if (this.isLoaded()){
                _local2.image = this.generateFinalImage(_arg1);
            };
            if (_local2.image != null){
                _local3 = null;
                if ((_local2 is MovieClip)){
                    _local3 = (_local2 as MovieClip);
                };
                if (this.forcedWidth == 0){
                    if (_local2.lowResImage){
                        _local2.forcedWidth = _local2.lowResImage.width;
                    } else {
                        _local2.forcedWidth = _local2.image.width;
                    };
                } else {
                    _local2.forcedWidth = this.forcedWidth;
                };
                if (this.offsetX){
                    _local2.offsetX = this.offsetX;
                };
                if (this.offsetY){
                    _local2.offsetY = this.offsetY;
                };
                _local2.forcedHeight = ((this.forcedHeight)>0) ? this.forcedHeight : _local2.image.height;
            } else {
                _local2 = null;
            };
            return (_local2);
        }
        protected function createBitmapDataFromMovieClip(_arg1:MovieClip):BitmapData{
            var _local2:BitmapData = new BitmapData(_arg1.width, _arg1.height, true, 0);
            var _local3:String = Global.stage.quality;
            GlobalEngine.stage.quality = StageQuality.HIGH;
            var _local4:MovieClip = (_arg1.getChildAt(0) as MovieClip);
            if (_local4){
                _local4.gotoAndPlay(0);
            };
            _local2.draw(_arg1, null, null, null, null, true);
            GlobalEngine.stage.quality = _local3;
            return (_local2);
        }
        protected function generateFinalImage(_arg1:MapResource):DisplayObject{
            var _local2:DisplayObject;
            var _local3:ItemImageAsset;
            var _local4:int;
            var _local5:int;
            var _local6:int;
            var _local7:Number;
            var _local8:ItemImageAsset;
            var _local9:ItemImageAsset;
            var _local10:ItemImageAsset;
            var _local11:String;
            var _local12:uint;
            var _local13:Loader;
            var _local14:ItemImage;
            var _local15:ItemImageInstance;
            var _local16:CompositeItemImage;
            var _local17:int;
            var _local18:ItemImage;
            var _local19:ItemImageInstance;
            if (this.isLoaded()){
                if (this.m_assets.length > 0){
                    _local3 = (this.m_assets[0] as ItemImageAsset);
                    _local4 = int(this.m_xml.@numFrames);
                    _local5 = int(this.m_xml.@frameWidth);
                    _local6 = int(this.m_xml.@frameHeight);
                    _local7 = Number(this.m_xml.@fps);
                    switch (this.m_className){
                        case "AnimatedBitmap":
                            _local2 = _local3.replicate(_local7);
                            if (_local2 == null){
                                _local2 = new AnimatedAsset(_local3.locator, _local3.bitmapData, _local4, _local5, _local6, _local7);
                                if (_arg1 != null){
                                    (_local2 as AnimatedAsset).enable = _arg1.doFramerateAnimOpt;
                                    (_local2 as AnimatedAsset).roundRobbin = _arg1.doRoundRobbinAnimOpt;
                                };
                                _local3.cache((_local2 as AnimatedAsset));
                            };
                            break;
                        case "AnimatedShaderBitmap":
                            _local8 = this.getAssetByName("animation");
                            _local9 = this.getAssetByName("texture");
                            _local2 = new AnimatedShaderBitmap(_local8.bitmapData, _local9.bitmapData, _local4, _local5, _local6, _local7);
                            if (_arg1 != null){
                                (_local2 as AnimatedShaderBitmap).enable = _arg1.doFramerateAnimOpt;
                                (_local2 as AnimatedShaderBitmap).roundRobbin = _arg1.doRoundRobbinAnimOpt;
                            };
                            break;
                        case "ColorMaskedBitmap":
                            _local10 = this.getAssetByName("mask");
                            if (((!((_local10 == null))) && (_local10.bitmapData))){
                                _local11 = String(this.m_xml.@color);
                                _local12 = 0xFFFFFF;
                                if (_local11.length > 0){
                                    _local12 = Utilities.hexColorToIntColor(_local11);
                                };
                                _local2 = new ColorMaskedBitmap(_local10.bitmapData, _local3.bitmapData, _local4, _local5, _local6, _local7, _local12);
                            };
                            break;
                        default:
                            if (_local3.bitmapData != null){
                                _local2 = new Bitmap(_local3.bitmapData);
                            } else {
                                if (_local3.contentClass != null){
                                    _local2 = new _local3.contentClass();
                                } else {
                                    if (((((_local3.isLoaded()) && (_local3.content))) && (_local3.content.loaderInfo))){
                                        _local13 = new Loader();
                                        _local13.loadBytes(_local3.content.loaderInfo.bytes);
                                        _local2 = _local13;
                                    };
                                };
                            };
                    };
                } else {
                    if (this.m_images.length > 0){
                        s_total++;
                        _local14 = this.m_images[0];
                        _local15 = _local14.getInstance(_arg1);
                        if (((_local15) && (_local15.image))){
                            _local15.image.x = _local14.offsetX;
                            _local15.image.y = _local14.offsetY;
                            _local15.image.name = _local14.type;
                            if (((((this.is_root) && ((this.m_images.length == 1)))) && (!((_local15.image is CompositeItemImage))))){
                                _local2 = (_local15.image as DisplayObject);
                                s_cached++;
                                if (this.is_road){
                                    this.offsetX = _local14.offsetX;
                                    this.offsetY = _local14.offsetY;
                                };
                                return (_local2);
                            };
                        };
                        _local16 = new CompositeItemImage();
                        if (((_local15) && (_local15.image))){
                            _local16.addChild((_local15.image as DisplayObject));
                        };
                        _local17 = 1;
                        while (_local17 < this.m_images.length) {
                            _local18 = this.m_images[_local17];
                            _local19 = _local18.getInstance(_arg1);
                            if (((_local19) && (_local19.image))){
                                _local19.image.x = _local18.offsetX;
                                _local19.image.y = _local18.offsetY;
                                _local19.image.name = _local18.type;
                                _local16.addChild((_local19.image as DisplayObject));
                            };
                            _local17++;
                        };
                        _local2 = _local16;
                    };
                };
            };
            return (_local2);
        }

    }
}//package Classes 
